package org.edu.modules.enroll.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.modules.enroll.entity.VeBaseSpecialty;
import org.edu.modules.enroll.entity.VeBaseStudent;
import org.edu.modules.enroll.entity.VeDictQuarter;
import org.edu.modules.enroll.entity.VeZsRegistration;
import org.edu.modules.enroll.service.VeBaseSpecialtyService;
import org.edu.modules.enroll.service.VeBaseStudentService;
import org.edu.modules.enroll.service.VeDictQuarterService;
import org.edu.modules.enroll.service.VeZsRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author lbh
 * @Date 2025/4/23 上午9:09
 * @Version 1.0
 */
@Api(tags="统计")
@RestController
@RequestMapping("enroll/statisticsInfo")
@Slf4j
public class StatisticsInfoController {
    @Autowired
    private VeBaseStudentService veBaseStudentService;
    @Autowired
    private VeDictQuarterService veDictQuarterService;
    @Autowired
    private VeZsRegistrationService veZsRegistrationService;
    @Autowired
    private VeBaseSpecialtyService veBaseSpecialtyService;



    @ApiOperation(value="统计男女生数")
    @GetMapping(value = "/statisticsSexNumber")
    public Result<List<JSONObject>> statisticsSexNumber(){
        List<JSONObject> list = new ArrayList<>();
        LambdaQueryWrapper<VeBaseStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VeBaseStudent::getXbm,1);
        Integer nanCount = veBaseStudentService.count(queryWrapper);
        JSONObject nanObject = new JSONObject();
        nanObject.put("value",nanCount);
        nanObject.put("name","男");
        list.add(nanObject);
        queryWrapper.clear();
        queryWrapper.eq(VeBaseStudent::getXbm,2);
        Integer nvCount = veBaseStudentService.count(queryWrapper);
        JSONObject nvObject = new JSONObject();
        nvObject.put("value",nvCount);
        nvObject.put("name","女");
        list.add(nvObject);

        return Result.OK(list);
    };

    @ApiOperation(value="统计报名及报到数")
    @GetMapping(value = "/statisticsApplyAndReported")
    public Result<Object> statisticsApplyAndReported(){
        QueryWrapper<VeDictQuarter> zsjWrapper = new QueryWrapper<VeDictQuarter>().eq("isCur", 1);
        VeDictQuarter one = veDictQuarterService.getOne(zsjWrapper);
        if(one == null){
            throw new RuntimeException("列表加载失败：当前招生季未启用！");
        }
        List<JSONObject> list = new ArrayList<>();
        Integer reportedCount = veZsRegistrationService.getReportStatisticsNum(one.getId(),null,null,1,"",null);
        JSONObject nanObject = new JSONObject();
        nanObject.put("value",reportedCount);
        nanObject.put("name","报到人数");
        list.add(nanObject);

        Integer applyCount = veZsRegistrationService.getReportStatisticsNum(one.getId(),null,null,null,"",null);
        JSONObject nvObject = new JSONObject();
        nvObject.put("value",applyCount);
        nvObject.put("name","报名人数");
        list.add(nvObject);
        return Result.OK(list);
    }

    @ApiOperation(value="统计报名及报到数")
    @GetMapping(value = "/statisticsApplyAndReportedGroupBySpecId")
    public Result<Object> statisticsApplyAndReportedGroupBySpecId(){
        QueryWrapper<VeDictQuarter> zsjWrapper = new QueryWrapper<VeDictQuarter>().eq("isCur", 1);
        VeDictQuarter one = veDictQuarterService.getOne(zsjWrapper);
        if(one == null){
            throw new RuntimeException("列表加载失败：当前招生季未启用！");
        }
        Map res = new HashMap();
        List<String> specNameList = new ArrayList<>();
        List<VeBaseSpecialty> veBaseSpecialtyList = veBaseSpecialtyService.list();
        specNameList = veBaseSpecialtyList.stream().map(VeBaseSpecialty::getZymc).collect(Collectors.toList());
        res.put("specNameList",specNameList);
        List<Integer> reportedRes = new ArrayList<>();
        List<Integer> noReportedRes = new ArrayList<>();
        for (VeBaseSpecialty veBaseSpecialty : veBaseSpecialtyList) {
            Integer reportedCount = veZsRegistrationService.getReportStatisticsNum(one.getId(),null,null,1,"",veBaseSpecialty.getId());
            reportedRes.add(reportedCount);
            Integer noReportedCount = veZsRegistrationService.getReportStatisticsNum(one.getId(),null,null,0,"",veBaseSpecialty.getId());
            noReportedRes.add(noReportedCount);
        }


        res.put("已报到",reportedRes);
        res.put("未报到",noReportedRes);

        return Result.OK(res);
    }
    @ApiOperation(value="统计毕业学校")
    @GetMapping(value = "/statisticsByxx")
    public Result<Object> statisticsByxx(){
        QueryWrapper<VeDictQuarter> zsjWrapper = new QueryWrapper<VeDictQuarter>().eq("isCur", 1);
        VeDictQuarter one = veDictQuarterService.getOne(zsjWrapper);
        if(one == null){
            throw new RuntimeException("列表加载失败：当前招生季未启用！");
        }
        Map res = new HashMap();
        List<String> specNameList = new ArrayList<>();
        List<VeBaseSpecialty> veBaseSpecialtyList = veBaseSpecialtyService.list();
        specNameList = veBaseSpecialtyList.stream().map(VeBaseSpecialty::getZymc).collect(Collectors.toList());
        res.put("specNameList",specNameList);
        List<Integer> reportedRes = new ArrayList<>();
        List<Integer> noReportedRes = new ArrayList<>();
        for (VeBaseSpecialty veBaseSpecialty : veBaseSpecialtyList) {
            Integer reportedCount = veZsRegistrationService.getReportStatisticsNum(one.getId(),null,null,1,"",veBaseSpecialty.getId());
            reportedRes.add(reportedCount);
            Integer noReportedCount = veZsRegistrationService.getReportStatisticsNum(one.getId(),null,null,0,"",veBaseSpecialty.getId());
            noReportedRes.add(noReportedCount);
        }


        res.put("已报到",reportedRes);
        res.put("未报到",noReportedRes);

        return Result.OK(res);
    }

}
